package com.zhihuichongcheng.application.ui.im.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.smartlib.cmnObject.recycleView.OnItemOnclick;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.bean.mine.UserBean;
import com.zhihuichongcheng.application.glide.GlideCircleTransform;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 3water on 2016/10/27.
 */

public class ContactAllAdapter extends RecyclerView.Adapter<ContactAllAdapter.ViewHold> {
    private List<UserBean> mListData = new ArrayList<>();
    private List<UserBean> mListSelect = new ArrayList<>();
    private Context mContext;
    private List<UserBean> mSelectData = new ArrayList<>();
    private OnItemOnclick<UserBean> onClickListener;

    public ContactAllAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public ViewHold onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.listadapter_group_contact, null);
        return new ViewHold(view);
    }

    @Override
    public void onBindViewHolder(final ViewHold holder, final int position) {
        final UserBean bean = mListData.get(position);
        Glide.with(mContext).load(bean.getLogoUrl()).placeholder(R.drawable.ic_default_avatar).transform(new GlideCircleTransform(mContext)).into(holder.mImAvatar);
        holder.mTvTitle.setText(mListData.get(position).getUserName());
        if (mSelectData.contains(bean)) {
            holder.mTvAlreadySelect.setVisibility(View.VISIBLE);
        } else {
            holder.mTvAlreadySelect.setVisibility(View.GONE);
        }
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.mTvAlreadySelect.getVisibility() == View.GONE) {
                    if (onClickListener != null) {
                        onClickListener.onClick(bean, position);
                    }
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mListData.size();
    }

    public void setData(List<UserBean> list) {
        this.mListData = list;
    }

    public boolean checkInList(UserBean easeUser) {
        for (int i = 0; i < mListSelect.size(); i++) {
            UserBean selectUSer = mListSelect.get(i);
            if (selectUSer.getUserName().equals(easeUser.getUserName())) {
                return true;
            }
        }
        return false;
    }

    public void setSelsetData(List<UserBean> list) {
        this.mSelectData = list;
    }

    public void setOnclick(OnItemOnclick<UserBean> onclick) {
        onClickListener = onclick;
    }

    public List<UserBean> getSelectData() {
        return mListSelect;
    }

    class ViewHold extends RecyclerView.ViewHolder {
        private ImageView mImAvatar;
        private View view;
        private TextView mTvTitle;
        private TextView mTvAlreadySelect;

        public ViewHold(View itemView) {
            super(itemView);
            view = itemView;
            mImAvatar = (ImageView) itemView.findViewById(R.id.im_avatar);
            mTvTitle = (TextView) itemView.findViewById(R.id.tv_name);
            mTvAlreadySelect = (TextView) itemView.findViewById(R.id.tv_already_select);
        }
    }
}
